<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

	<%-- Java imports --%>
	<%@ page import="java.io.*" %>
	<%@ page import="java.util.*" %>
	<%@ page import="com.tpix.PixieCore" %>
	
	<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
	<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
	<%@ page import="com.google.appengine.api.datastore.Entity" %>
	<%@ page import="com.google.appengine.api.datastore.PreparedQuery" %>
	<%@ page import="com.google.appengine.api.datastore.Query" %>
	<%@ page import="com.google.appengine.api.users.UserService" %>
	<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

	<!-- Get references to Java objects -->
	<%
		PixieCore core = (PixieCore) request.getSession().getAttribute("core");
		if (core == null) {
			core = new PixieCore();
			request.getSession().setAttribute("core", core);
		}
		%>
	<%-- Detect and redirect a user who is already logged in --%>
	<% 		
		UserService userService = UserServiceFactory.getUserService();
		String thisURL = request.getRequestURI();
		response.setContentType("text/html") ;
		if (request.getUserPrincipal() != null) {
			response.sendRedirect("/");
		}		
	 	%>
	  	
	<title>Pixie - Log In</title>

		<!-- CSS -->
		<link type="text/css" rel="stylesheet" href="/stylesheets/upload.css" />
		<link type="text/css" rel="stylesheet" href="/stylesheets/960.css" />
		
		<!-- TypeKit -->
		<script type="text/javascript" src="http://use.typekit.com/waa6dbj.js"></script>
		<script type="text/javascript">try{Typekit.load();}catch(e){}</script>

</head>
<body>
	<div class="container_12">
		<div id="wrapper">
				
			<div id="logo">
			<img src="/images/pixie_home.png" alt="Hello, Pixie"/>
			</div>
				
				
				<!-- Login Link -->
				<div id="login">
				<h2><strong>
				<%		userService = UserServiceFactory.getUserService();
						thisURL = request.getRequestURI();
						response.setContentType("text/html") ;
						if (request.getUserPrincipal() != null) {
							out.println("<p>Hello, " +
									request.getUserPrincipal().getName() + 
									"! You can <a href=\"" + 
									userService.createLogoutURL(thisURL) + 
									"\">sign out</a>.<p>");
						}
						else {
							out.println("<p>Please <a href=\"" +
									userService.createLoginURL(thisURL) +
									"\">Sign in</a><p>");
						}
					%>	
					</strong></h2>
				</div>
		</div>
	</div>

</body>
</html>